define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    
    require('../su/accordion/oaTabCtrl');
    
    require('../announcement/dialogs/announcementCreateDialogCtrl');
    var uService = require('core/services/biz/noticeService');
    var getTimeToString = uService.getTimeToString;
    $.widget('rk.announcementPanelCtrl', $.rk.oaPanelCtrl, {
        _language: "zh",
        //create方法中用来生成UI
        _create: function () {
            var me = this;
            var elem = me.element;
            if (navigator.userLanguage) {
                me._language = navigator.userLanguage.substring(0, 2).toLowerCase();
            } else {
                me._language = navigator.language.substring(0, 2).toLowerCase();
            }
            var temp = require('oa/tmpl/announcement/tpl_announcement_panel.tpl');
            var html = rk.templateText(temp,{});
            //将html显示在elem中
            elem.html(html);
            me._listeners();
        },
        //init方法中，用来初始化数据
        _init: function () {
            var me = this;
            var elem = me.element;
            me.loaddata("announcement");
        },
        initData: null,
        accordion: null,
        _listeners: function () {
            var me = this;
            var elem = me.element;
            me._detailListener();
            me._announcementQueryListener();
            me._create_listener();
        },
        _detailListener: function () {
            var me = this;
            var elem = me.element;
            elem.on('click', "[act='annoRow']", function () {
                var row = $(this);
                me.popupContent({
                    defaultScroll: false,
                    size:"middle",
                    onSuccess: function (contentElem) {
                        //announcementContentCtrl
                        require.async(["./announcementContentCtrl.js"],function(c){
                            contentElem.announcementContentCtrl({id: row.attr("aid"), type: "announcement"});
                        });
                    }
                });
                elem. find( "[act='annoRow']"). removeClass( "show-content-hover");
                row. addClass( "show-content-hover" );
            });
        },
        _create_listener: function () {
            var me = this;
            var elem = me.element;
            elem.on('click', "[act='anno_create_new']", function () {
                me._showCreate();
            });
            elem.on('click','.oa-close-panel',function(){
                rk.oa.closePanel();
            });
        },
        _showCreate: function () {
            rk.dialog({
                title: rk.i18n('ANNOUNCEMENT_POST'),
                width:"930",
                success: function (contentElem) {
                    contentElem.announcementCreateDialogCtrl();//初始化弹框内容的Ctrl
                },
                footer: ['cancel', {
                    act: 'ok', visible: true, text: rk.i18n('SAVE')
                }]
            });
        },
        _announcementQueryListener: function () {
            var me = this;
            var elem = me.element;
            //初始化  部门公告查询列表
            var ann_condition = elem.find('[act="announcement_condition"]');
            ann_condition.show();
            ann_condition.find('[act="ann_condition_dep"]').selectmenu();
            ann_condition.find('[act="ann_condition_type"]').selectmenu();
            rk.post("/json/oa_announcement/initGroup.action", {pageNo: 1, pageSize: 20}, function (json) {
                if (json.status === 0) {
                    var dataList = json.groupList;
                    var html = "<option value=''>"+rk.i18n('ANNOUNCEMENT_ALL_DEPART')+"</option>";
                    $.each(dataList, function (i, dataJson) {
                        html += "<option value='" + dataJson.id + "'>" + dataJson.name + "</option>";
                    });
                    elem.find("[act='ann_condition_dep']").html(html);
                    elem.find("[act='ann_condition_dep']").attr("isload","true");
                    ann_condition.find('[act="ann_condition_dep"]').selectmenu('refresh', true);




                    dataList = json.typeList;
                    if(dataList.length > 0) {
                        var html = "<option value=''>"+rk.i18n('ANNOUNCEMENT_ALL_TYPE')+"</option>";
                        $.each(dataList, function (i, dataJson) {
                            html += "<option value='" + dataJson.id + "'>" + dataJson.name + "</option>";
                        });
                        elem.find("[act='ann_condition_type']").html(html);
                        elem.find("[act='ann_condition_type']").attr("isload", "true");
                        ann_condition.find('[act="ann_condition_type"]').selectmenu('refresh', true);
                    }else{
                        ann_condition.find('[act="ann_condition_type"]').hide();
                    }
                    if(json.isDepartAdmin){
                        elem.find("[act='anno_create_new']").show();
                    }else{
                        elem.find("[act='anno_create_new']").hide();
                    }
                } else {
                    rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                }
            });
            ann_condition.find('[act="ann_condition_dep"]').selectmenu({
                change: function () {
                    $.proxy(me._ann_condition_type_depQuery(), me);
                }
            });
            ann_condition.find('[act="ann_condition_type"]').selectmenu({
                change: function () {
                    $.proxy(me._ann_condition_type_depQuery(), me);
                }
            });

            elem.on('click', "[act='ann_search_btn']", function () {
                //elem.find('[act="ann_announce-title"]').hide();
                elem.find('[act="ann_announce-search-box"]').animate({left:"100%",right:"-100%"},0,null,function(){
                    elem.find('[act="ann_announce-search-box"]').show();
                    elem.find('[act="ann_announce-search-box"]').animate({left:0,right:0},300,null,function(){
                        elem.find("[act='ann_condition_title_name']").focus();
                    });
                });
            });
            //这样写法，为了后面可以移除事件
            elem.find("[act='ann_condition_title_name']").bind("blur", function () {
                elem.find('[act="ann_announce-search-box"]').animate({left:"100%",right:"-100%"},300,null,function(){
                    elem.find('[act="ann_announce-search-box"]').hide();
                });
            });
            var title_search = function () {
                var titleElem = elem.find("[act='ann_condition_title_name']");
                var title = titleElem.val();
                var param = {pageNo: 1, pageSize: 20};
                if (title) {
                    param.title = title;
                    me.loaddata("announcement_titleQuery", param);
                }
            };
            elem.on('mousedown', '[act="ann_title_name_search"]', function (e) {
                e.preventDefault();
                title_search();
            });
            elem.on('keydown', '[act="ann_condition_title_name"]', function (e) {
                if (e.keyCode == 13) {
                    title_search();
                }
            });
            var clear_ann_title_name = function(){
                elem.find("[act='ann_condition_title_name']").val("");
                elem.find("[act='ann_title_text_clear']").hide();
            };
            elem.on('click', '[act="ann_search_return_table"]', function () {
                me.loaddata("announcement");
                me._announcement_refresh();
                clear_ann_title_name();
            });
            elem.on('mousedown', "[act='ann_title_text_clear']", function (e) {
                e.preventDefault();
                clear_ann_title_name();
            });
            elem.on('input propertychange', "[act='ann_condition_title_name']", function(){
                var val = elem.find("[act='ann_condition_title_name']").val();
                if(val){
                    elem.find("[act='ann_title_text_clear']").show();
                }else{
                    elem.find("[act='ann_title_text_clear']").hide();
                }
            });
        },
        _announcement_refresh: function () {
            var me = this;
            var elem = me.element;
            var ann_condition = elem.find('[act="announcement_condition"]');
            elem.find('[act="ann_announce-title"]').show();
            elem.find('[act="ann_announce-search-box"]').hide();
            ann_condition.find('[act="ann_condition_dep"]').val("");
            ann_condition.find('[act="ann_condition_type"]').val("");
            ann_condition.find('[act="ann_condition_dep"]').selectmenu('refresh', true);
            ann_condition.find('[act="ann_condition_type"]').selectmenu('refresh', true);
            //重新绑定事件
            elem.find("[act='ann_condition_title_name']").bind("blur", function () {
                //elem.find('[act="ann_announce-title"]').animate({left:"-100%"},0);
                //elem.find('[act="ann_announce-title"]').animate({left:0},"slow");
                elem.find('[act="ann_announce-search-box"]').animate({left:"100%",right:"-100%"},300,null,function(){
                    elem.find('[act="ann_announce-search-box"]').hide();
                })
            });
        },
        _ann_condition_type_depQuery: function () {
            var me = this;
            var elem = me.element;
            var param = {pageNo: 1, pageSize: 20};
            var ann_condition = elem.find('[act="announcement_condition"]');
            var groupId = ann_condition.find('[act="ann_condition_dep"]').val();
            var typeId = ann_condition.find('[act="ann_condition_type"]').val();
            if (groupId) {
                param.queryGroupId = groupId;
            }
            if (typeId) {
                param.queryTypeId = typeId;
            }
            me.loaddata("announcement", param);
        },
        loaddata: function (type, param, divs) {
            var me = this;
            var elem = me.element;
            elem.find("[act='announcement_page_parent']").hide();
            //加载部门公告
            if (type === "announcement") {
                elem.find('[table_name="announcement"]').beLoading();
                elem.find('[act="announcement_page"]').hide();
                if (!param) {
                    param = {pageNo: 1, pageSize: 20};
                }
                rk.post("/json/oa_announcement/all.action", param, function (json) {
                    if (json.status === 0) {
                        var dataList = json.body.announcements;
                        if (dataList.length > 0) {
                            var listHtml = "";
                            $.each(dataList, function (i, dataJson) {
                                listHtml += me.getAnnouncementRowHtml(json.nowTime, dataJson);
                            });
                            elem.find('[table_name="announcement"]').html(listHtml);
                            elem.find('[act="announcement_page"]').show();
                        } else {
                            //var html = rk.template("tpl_announcement_none_panel", {text: "没有部门公告"});
                            var temp = require('oa/tmpl/announcement/tpl_announcement_none_panel.tpl');
                            var html = rk.templateText(temp,{text: rk.i18n("ANNOUNCEMENT_NO_DEPART")});
                            elem.find('[table_name="announcement"]').html(html);
                            elem.find('[act="announcement_page"]').hide();
                        }
                        elem.find('[act="ann_search_operation"]').hide();
                        elem.find("[act='announcement_page_parent']").show();
                        elem.find('[act="announcement_page"]').rkPaging({
                            pagenum: json.body.pageNo,  //（可选）当前页数，默认1
                            pagesize: json.body.pageSize,  //（可选）当前每页显示条数，默认20
                            rowscount: json.body.dataCount, //所有数据数
                            onChange: function (pagenum) {
                                var params = param;
                                params.pageNo = pagenum;
                                params.pageSize = json.body.pageSize;
                                me.loaddata(type, params, divs)
                            }
                        });
                    } else {
                        rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    }
                    elem.find('[act="beScroll"]').beScroll();
                });

            }
            if (type === "announcement_titleQuery") {
                elem.find('[table_name="announcement"]').beLoading();
                elem.find('[act="announcement_page"]').hide();
                if (!param) {
                    param = {pageNo: 1, pageSize: 20};
                }
                rk.post("/json/oa_announcement/all.action", param, function (json) {
                    if (json.status === 0) {
                        var dataList = json.body.announcements;
                        elem.find("[act='ann_search_number']").html(json.body.dataCount);
                        if (dataList.length > 0) {
                            var listHtml = "";
                            $.each(dataList, function (i, dataJson) {
                                listHtml += me.getAnnouncementRowHtml(json.nowTime, dataJson);
                            });
                            elem.find('[table_name="announcement"]').html(listHtml);
                            elem.find('[act="announcement_page"]').show();
                        } else {
                            //var html = rk.template("tpl_notice_announcement_none_panel", {text: "没有符合条件的公告"});
                            var temp = require('oa/tmpl/announcement/tpl_announcement_none_panel.tpl');
                            var html = rk.templateText(temp,{text: rk.i18n('ANNOUNCEMENT_QUERY_NOT_HAVE')});
                            elem.find('[table_name="announcement"]').html(html);
                            elem.find('[act="announcement_page"]').hide();
                        }
                        elem.find('[act="ann_search_operation"]').show();
                        elem.find("[act='ann_condition_title_name']").unbind("blur");//移除焦点事件
                        elem.find("[act='announcement_page_parent']").show();
                        elem.find('[act="announcement_page"]').rkPaging({
                            pagenum: json.body.pageNo,  //（可选）当前页数，默认1
                            pagesize: json.body.pageSize,  //（可选）当前每页显示条数，默认20
                            rowscount: json.body.dataCount, //所有数据数
                            onChange: function (pagenum) {
                                var params = param;
                                params.pageNo = pagenum;
                                params.pageSize = json.body.pageSize;
                                me.loaddata(type, params, divs)
                            }
                        });
                    } else {
                        rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    }
                });
            }
        },
        getAnnouncementRowHtml: function (nowTime, dataJson) {
            var me = this;
            var elem = me.element;
            if (dataJson.created) {
                dataJson.created = me.getTimeToString(dataJson.created, nowTime, me);
            }
            var temp = require('oa/tmpl/announcement/template/tpl_announcement_table_row.tpl');
            var html = rk.templateText(temp,dataJson);
            return html;
        },
        getTimeToString: getTimeToString
    });
});